package net.xdclass.order.component;

import net.xdclass.order.enums.ProductOrderPayTypeEnum;
import net.xdclass.order.model.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PayFactory {

    @Autowired
    private List<PayStrategy> strategys = new ArrayList<>();

    Map<ProductOrderPayTypeEnum, PayStrategy> map = new HashMap<>();

    @PostConstruct
    public void init(){
        for (PayStrategy strategy : strategys) {
            map.put(strategy.supportType(), strategy);
        }
    }

    /**
     * 下单
     * @param payInfo
     * @return
     */
    public String pay(PayInfoVO payInfo){
        ProductOrderPayTypeEnum payType = payInfo.getPayType();
        if (map.isEmpty() || payType == null){
            return "";
        }
        return map.get(payType).unifiedOrder(payInfo);
    }

    /**
     * 查询订单状态
     */
    public String queryOrderState(PayInfoVO payInfo){
        ProductOrderPayTypeEnum payType = payInfo.getPayType();
        if (map.isEmpty() || payType == null) {
            return "";
        }
        return map.get(payType).queryPayState(payInfo);
    }

}
